package com.wcpdoc.api.controller;

import com.alibaba.excel.EasyExcel;
import com.wcpdoc.api.entity.UserEntity;
import com.wcpdoc.base.entity.Label;
import com.wcpdoc.base.entity.Org;
import com.wcpdoc.base.entity.User;
import com.wcpdoc.base.service.LabelService;
import com.wcpdoc.base.service.OrgService;
import com.wcpdoc.base.service.UserExService;

import com.wcpdoc.core.controller.BaseController;
import com.wcpdoc.core.entity.PageIn;
import com.wcpdoc.core.entity.PageOut;
import com.wcpdoc.core.entity.PageResult;
import com.wcpdoc.core.entity.PageResultEx;
import com.wcpdoc.core.exception.MyException;
import com.wcpdoc.core.service.OnlineUserService;
import com.wcpdoc.core.util.StringUtil;
import com.wcpdoc.core.util.ValidateUtil;
import com.wcpdoc.file.service.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制层
 * <p>
 * v1.0 zhanghc 2016-6-15下午17:24:19
 */
@Controller
@RequestMapping("/api/label")
public class ApiLabelController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(ApiLabelController.class);

    @Resource
    private LabelService labelService;


    /**
     * 用户列表
     *
     * @return PageOut
     */
    @RequestMapping("/listpage")
    @ResponseBody
    public PageResult listpage() {
        try {
            PageIn pageIn = new PageIn(request);
            log.info("====查询标签列表");
            log.info(pageIn.toString());
            PageOut pageOut = labelService.getListpage(pageIn);
            return PageResultEx.ok().data(pageOut);
        } catch (Exception e) {
            log.error("标签列表错误：", e);
            return PageResult.err();
        }
    }


    /**
     * 用户添加
     * <p>
     * v1.0 zhanghc 2016-6-15下午17:24:19
     *
     * @param label
     * @return PageResult
     */
    @RequestMapping("/add")
    @ResponseBody
    public PageResult add(Label label) {
        try {
            log.info(label.toString());
            // 用户添加
            labelService.add(label);
            return PageResultEx.ok();
        } catch (MyException e) {
            log.error("标签添加错误：{}", e.getMessage());
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            log.error("标签添加错误：{}", e.getMessage());
            return PageResult.err().msg(e.getMessage());
        }
    }

    /**
     * 用户修改
     * <p>
     * v1.0 zhanghc 2016-6-15下午17:24:19
     *
     * @param label
     * @return PageResult
     */
    @RequestMapping("/edit")
    @ResponseBody
    public PageResult edit(Label label) {
        try {
            // 用户修改
            Label entity = labelService.getEntity(label.getId());
            labelService.edit(label);
            return PageResultEx.ok();
            /*  String oldLoginName = entity.getLoginName();*/
         /*   userService.editEx(user);
            String newLoginName = entity.getLoginName();*/

        /*    // 更新密码
            Map<String, Object> data = new HashMap<String, Object>();
            if (!oldLoginName.equals(newLoginName)) {
                String initPwd = userService.pwdInit(user.getId());
                data.put("initPwd", initPwd);
            }*/


//            return PageResultEx.ok().data(data);
        } catch (MyException e) {
            log.error("标签修改错误：{}", e.getMessage());
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            log.error("标签修改错误：", e);
            return PageResult.err();
        }
    }

    /**
     * 用户删除
     * <p>
     * v1.0 zhanghc 2016-6-15下午17:24:19
     *
     * @param id
     * @return PageResult
     */
    @RequestMapping("/del")
    @ResponseBody
    public PageResult del(Integer id) {
        try {
            labelService.del(id);
            return PageResult.ok();
        } catch (MyException e) {
            log.error("标签删除错误：{}", e.getMessage());
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            log.error("标签删除错误：", e);
            return PageResult.err();
        }
    }

    /**
     * 用户获取
     * <p>
     * v1.0 zhanghc 2016-6-15下午17:24:19
     *
     * @param id
     * @return PageResult
     */
    @RequestMapping("/get")
    @ResponseBody
    public PageResult get(Integer id) {
        try {
            if (!ValidateUtil.isValid(id)) {
                throw new MyException("参数错误：id不能为空");
            }
          /*  if (!ValidateUtil.isValid(id)) {
                id = getCurUser().getId();
            }*/
            Label label = labelService.getEntity(id);
            PageResultEx pageResult = PageResultEx.ok()
                    .addAttr("id", label.getId())
                    .addAttr("name", label.getName());
            return pageResult;
        } catch (MyException e) {
            log.error("用户获取错误：{}", e.getMessage());
            return PageResult.err().msg(e.getMessage());
        } catch (Exception e) {
            log.error("用户获取错误：", e);
            return PageResult.err();
        }
    }

}
